import {
  resellList,
  userConfirmDelivery
} from "../../../../api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type_one: 0,
    list: [],
    table_on: 0,
    little_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type
    this.setData({
      type_one: type
    })
    wx.showLoading({
      title: '正在加载...',
    })
    this.init(type)
  },
  init(type) {
    var that = this
    that.data.list = []
    resellList(
      getApp().globalData.Store.token
    ).then(res => {
      wx.hideLoading()
      var lis = res.data.data
      for (var i = 0; i < lis.length; i++) {
        if (type == 1) {
          if (lis[i].product_resell_state == 7) {
            that.data.list.push(lis[i])
          }
        }
        if (type == 2) {
          if (lis[i].product_resell_state <= 6) {
            that.data.list.push(lis[i])
          }
        }
        if (type == 3) {
          if (lis[i].product_resell_state == 8 || lis[i].product_resell_state == 9) {
            that.data.list.push(lis[i])
          }
        }
        if (type == 4) {
          if (lis[i].product_resell_state == 10) {
            that.data.list.push(lis[i])
          }
        }
        if (i == lis.length - 1) {
          wx.hideLoading()
          that.setData({
            list: that.data.list,
            little_list: that.data.list
          })
        }
      }
    })
  },
  tabLis(e) {
    var type = e.currentTarget.dataset.type
    this.tabList_btn(type)
  },
  tabList_btn(num){
    var that = this
    var list = that.data.list
    var new_list = []
    if (num == that.data.table_on) {
      return
    } else {
      if (num == 0) {
        new_list = that.data.list
        that.setData({
          table_on: num,
          little_list: new_list
        })
      }
      if (num == 5) {
        for (var i = 0; i < list.length; i++) {
          if (list[i].product_resell_state == 5 || list[i].product_resell_state == 6) {
            new_list.push(list[i])
          }
          if (i == list.length - 1) {
            that.setData({
              table_on: num,
              little_list: new_list
            })
          }
        }
      } else {

        for (var i = 0; i < list.length; i++) {
          if (list[i].product_resell_state == num) {
            new_list.push(list[i])
          }
          if (i == list.length - 1) {
            that.setData({
              table_on: num,
              little_list: new_list
            })
          }
        }
      }
    }
  },
  golis(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/consignment/detail/detail?id=' + id,
    })
  },
  goBank() {
    wx.navigateTo({
      url: '/pages/myList/bankCard/bankCard',
    })
  },
  onShow: function () {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
  gofahuo(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/consignment/delivery/delivery?id=' + id,
    })
  },
  confirm(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success(res) {
        if (res.confirm) {
          userConfirmDelivery(
            getApp().globalData.Store.token,
            id,
          ).then(res => {
            if (res.data.code == 666) {
              that.init(that.data.type_one)
              that.setData({
                table_on:0
              })
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          })
        } else if (res.cancel) {
         
        }
      }
    })
  },
  lis_none_btn(){
    wx.navigateTo({
      url: '/pages/consignment/newConsignment/newConsignment',
    })
  }
})